package com.pets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pets.entity.DownloadRecord;
import com.pets.entity.FileInformation;
import com.pets.entity.Users;
import com.pets.mapper.DownloadRecordMapper;
import com.pets.mapper.FileInformationMapper;
import com.pets.mapper.UsersMapper;
import com.pets.service.IDownloadRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 下载记录表 服务实现类
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-09
 */
@Service
public class DownloadRecordServiceImpl extends ServiceImpl<DownloadRecordMapper, DownloadRecord> implements IDownloadRecordService {

    @Autowired
    private DownloadRecordMapper downloadRecordMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private FileInformationMapper fileInformationMapper;

    /**
     * 创建下载记录
     * @param fileId
     * @param userId
     * @return
     */
    @Override
    public Result download(Integer fileId, Integer userId) {

        if(userId==null){
            return Result.fail("未获取到用户，检查是否登陆！");
        }
        Users users = usersMapper.selectById(userId);
        if(users==null){
            return Result.fail("未查询到用户！");
        }
        FileInformation fileInformation = fileInformationMapper.selectById(fileId);
        if(fileInformation==null){
            return Result.fail("未查询到文件！");
        }
        //判断文件是否公开
        if(!fileInformation.getIsOpen()){
            return Result.fail("文件未公开，不允许下载！");
        }
        DownloadRecord downloadRecord = new DownloadRecord();
        downloadRecord.setUserId(users.getUserId());
        downloadRecord.setFileId(fileInformation.getFileId());
        downloadRecord.setCreateTime(LocalDateTime.now());
        downloadRecord.setFileName(fileInformation.getFileName());
        downloadRecord.setDeleted(false);
        if(downloadRecordMapper.insert(downloadRecord)>0){
            if(!users.getIsAdministrators()){
                Integer integral = users.getIntegral();
                integral--;
                users.setIntegral(integral);
                if(usersMapper.updateById(users)>0){
                    return Result.success("允许下载");
                }else{
                    return Result.success("允许下载，积分减少失败，请联系开发人员！");
                }
            }
            return Result.success("允许下载");
        }else {
            return Result.fail("创建下载记录失败");
        }
    }

    /**
     * 分页查询
     */
    @Override
    public Result selectPage(Integer page, Integer pageSize, String fileName, Integer userId) {
        LambdaQueryWrapper<DownloadRecord> lqw = new LambdaQueryWrapper<>();
        lqw.like(fileName != null && !fileName.isEmpty(), DownloadRecord::getFileName, fileName)
                .eq(userId!=null,DownloadRecord::getUserId,userId)
                .eq(DownloadRecord::getDeleted,false)
                .orderByDesc(DownloadRecord::getCreateTime);
        IPage<DownloadRecord> Page = new Page<>(page, pageSize);
        IPage<DownloadRecord> IPage = downloadRecordMapper.selectPage(Page, lqw);
        return Result.success("查询成功", IPage);
    }

    /**
     * 删除下载记录
     */
    @Override
    public Result deleteById(Integer id) {
        return downloadRecordMapper.deleteById(id)>0?Result.success("删除成功！"):Result.fail("删除失败！");
    }
}
